//
//  PiLiangGuanLiViewController.m
//  EJLX
//
//  Created by xufei on 15/12/10.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "PiLiangGuanLiViewController.h"
#import "SearchViewController.h"
#import "HttpsManager.h"
#import "SecondShangPinGuanLi.h"
#import "PiLiangGuanLiTableViewCell.h"
@interface PiLiangGuanLiViewController ()

@end

@implementation PiLiangGuanLiViewController
-(void)viewWillAppear:(BOOL)animated
{
    [self request];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    _isSelect=NO;
    _xuanZhongAry=[NSMutableArray array];

    _isSelectAll=NO;
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0,20,WIDE, 44)];
    view.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:view];
    UIButton*backBut=[[UIButton alloc]initWithFrame:CGRectMake(10,7, 30,30)];
    [backBut setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(backButClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backBut];
    UIButton*shaiXuan=[[UIButton alloc]initWithFrame:CGRectMake(WIDE-50, 10,40, 24)];
    [shaiXuan setTitle:@"取消" forState:UIControlStateNormal];
    shaiXuan.font=[UIFont systemFontOfSize:12];
    [shaiXuan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [shaiXuan addTarget:self action:@selector(shaiXuanBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:shaiXuan];
    
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-50, 10,100, 24)];
    label.text=@"我的商品";
    label.textColor=[UIColor whiteColor];
    label.textAlignment=NSTextAlignmentCenter;
    [view addSubview:label];
    
   // UILabel*label1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,40, 20)];
    //label1.text=@"商品";
    //l/abel1.textColor=[UIColor grayColor];
    // [_textField addTarget:self action:@selector(textFieldBut) forControlEvents:UIControlEventTouchUpInside];
    UIButton*textFieldBut=[[UIButton alloc]initWithFrame:CGRectMake(0,0,WIDE,44)];
    
    [textFieldBut addTarget:self action:@selector(textFieldBut) forControlEvents:UIControlEventTouchUpInside];
    [_search addSubview:textFieldBut];
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,70+44, WIDE, HEIGHT-70-50-44) style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.rowHeight=170;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"PiLiangGuanLiTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    for (int i=0; i<4; i++)
    {
        UIButton*lastBut=[[UIButton alloc]initWithFrame:CGRectMake(30+(WIDE-20)/4*i,HEIGHT-50,50,20)];
        NSArray*ary=[NSArray arrayWithObjects:@"全选",@"上架.jpg",@"下架.jpg",@"删除.jpg", nil];
        lastBut.tag=i;
        
        [lastBut addTarget:self action:@selector(lastButClick:) forControlEvents:UIControlEventTouchUpInside];
        [lastBut setBackgroundImage:[UIImage imageNamed:ary[i]] forState:UIControlStateNormal];
        if (i==0)
        {
            [lastBut setBackgroundImage:[UIImage imageNamed:@"取消选择"] forState:UIControlStateSelected];
        }
        //         NSArray*ary1=[NSArray arrayWithObjects:@"取消选择",@"上架.jpg",@"下架.jpg",@"删除.jpg", nil];
        //        [lastBut setBackgroundImage:[UIImage imageNamed:ary1[i]] forState:UIControlStateSelected];
        [self.view addSubview:lastBut];
    }
    
    
}
-(void)lastButClick:(UIButton*)sender
{
    if (sender.tag==0)
    {
        UIButton *btn = (UIButton *)sender;
        btn.selected = !btn.selected;
        
        if (btn.selected) {
            _isSelectAll = YES;
            _isSelect = YES;
            for (int i = 0; i < [_selectAry[_selectIndex] allKeys].count; i++) {
                [_selectAry[_selectIndex] removeObjectForKey:[NSString stringWithFormat:@"%d",i ]];
                [_selectAry[_selectIndex] setObject:@"YES" forKey:[NSString stringWithFormat:@"%d",i ]];
                SecondShangPinGuanLi*sec=_dataAry[i];
                NSString*productS=[NSString stringWithFormat:@"%d",sec.productId];
                NSLog(@"productid  %d",sec.productId);
                [_xuanZhongAry addObject:productS];
                NSLog(@"---%lu",(unsigned long)_xuanZhongAry.count);
            }
            NSLog(@"zzzz%@",_xuanZhongAry);
            
            //  [btn setImage:[UIImage imageNamed:@"全选"] forState:UIControlStateSelected];
            
        }else {
            _isSelectAll = NO;
            _isSelect = NO;
            for (int i = 0; i < [_selectAry[_selectIndex] allKeys].count; i++) {
                [_selectAry[_selectIndex] removeObjectForKey:[NSString stringWithFormat:@"%d",i ]];
                [_selectAry[_selectIndex] setObject:@"NO" forKey:[NSString stringWithFormat:@"%d",i ]];
                [btn setImage:[UIImage imageNamed:@"取消全选"] forState:UIControlStateNormal];
                SecondShangPinGuanLi*sec=_dataAry[i];
                NSString*pr=[NSString stringWithFormat:@"%d",sec.productId];
                [_xuanZhongAry removeObject:pr];
               

            }
            NSLog(@"azzzz%@",_xuanZhongAry);

        }
        
        [_tableView reloadData];
    }
    
    
    
    if (sender.tag==1)
    {
        NSLog(@"-----选择的个数  %lu",(unsigned long)_xuanZhongAry.count);
        NSLog(@"-----%@",_xuanZhongAry);
        [HttpsManager requestGetShangJiaShangPinProducts:_xuanZhongAry requestUrl:@"add" Competion:^(int errcode, NSString *errmsg)
         {
             
             
             
             SHOW_ALERT(errmsg);
             [_tableView reloadData];
         }];
        
        
        
        
        
        
    }
    if (sender.tag==2)
    {
        [HttpsManager requestGetShangJiaShangPinProducts:_xuanZhongAry requestUrl:@"rem" Competion:^(int errcode, NSString *errmsg)
         {
            
             SHOW_ALERT(errmsg);
             
             
             [_tableView reloadData];
         }];
        

    }
    if (sender.tag==3)
    {
        if (_xuanZhongAry>0) {
            
            for (int i= 0; i <_xuanZhongAry.count; i++) {
                
                [HttpsManager requestGetShangJiaShangPinProducts:_xuanZhongAry requestUrl:@"del" Competion:^(int errcode, NSString *errmsg)
                 {
                     
         
                 }];
                UITableViewCell *cell = (UITableViewCell*)[[[sender superview]superview] superview];
                NSIndexPath *index = [_tableView indexPathForCell:cell];
                [_dataAry removeObjectAtIndex:index.row];
                
                [_tableView reloadData];
                
            }
            

            }
           
    }
}
- (void)requestEnd{
    [_tableView.pullToRefreshView stopAnimating];
    
    [_tableView.infiniteScrollingView stopAnimating];
    
}
-(void)request
{
    [HttpsManager registerWoDeShangPin:_shopId type:@"1" completion:^(int errcode, ShangPinGuanLi *guanLi)
     {
         if (guanLi!=nil)
         {
             _guanLi=guanLi;
             _dataAry=_guanLi.listAry;
             
             _selectAry=[NSMutableArray array];
             NSMutableDictionary*goodDic=[[NSMutableDictionary alloc]init];
             for (int i=0; i<_dataAry.count; i++)
             {
                 [goodDic setObject:@"NO" forKey:[NSString stringWithFormat:@"%d",i]];
                 [_selectAry addObject:goodDic];
             }
             __weak PiLiangGuanLiViewController *weakSelf = self;
             [_tableView addPullToRefreshWithActionHandler:^{
                 NSLog(@"当进行下拉刷新执行该方法");
                 [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
             }];
             
             [_tableView addInfiniteScrollingWithActionHandler:^{
                 [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
             }];

             
             
             
             [_tableView reloadData];

         }
         else
         {
             if (errcode==ERRCODE_SHOPNOT)
             {
                 SHOW_ALERT(@"您的店铺不存在");
                 [self.navigationController popViewControllerAnimated:YES];
             }
             else
             {
                 LoginVC*vc=[[LoginVC alloc]init];
                 [self.navigationController pushViewController:vc animated:YES];
                 
             }
         }
         
     }];

//    [HttpsManager registerWoDeShangPin:_shopId type:@"1" completion:^(ShangPinGuanLi *guanLi)
//     {
//         _guanLi=guanLi;
//         _dataAry=_guanLi.listAry;
//         
//         _selectAry=[NSMutableArray array];
//         NSMutableDictionary*goodDic=[[NSMutableDictionary alloc]init];
//         for (int i=0; i<_dataAry.count; i++)
//         {
//             [goodDic setObject:@"NO" forKey:[NSString stringWithFormat:@"%d",i]];
//             [_selectAry addObject:goodDic];
//         }
//         
//         
//         
//         
//         [_tableView reloadData];
//         
//     }];
    
}
-(void)shaiXuanBut
{
    
    
    [self dismissViewControllerAnimated:YES completion:^
     {
         nil;
     }];
    
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // else
    // {
    SecondShangPinGuanLi*guanLi=_dataAry[indexPath.row];
    PiLiangGuanLiTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell.prcture sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",guanLi.imgUrl]] placeholderImage:[UIImage imageNamed:@"头像1"]];
    
    cell.fenXiao.text=guanLi.title;
    if (guanLi.discountPrice)
    {
        cell.shouJia.text=[NSString stringWithFormat:@"%.2f",guanLi.discountPrice*0.01];
        
    }
    else
    {
        cell.shouJia.text=[NSString stringWithFormat:@"%.2f",guanLi.price*0.01];
    }
    cell.yongJin.text=[NSString stringWithFormat:@"%.2f",guanLi.commision*0.01];
    cell.kuCun.text=[NSString stringWithFormat:@"%d",guanLi.stockNum];
    cell.xuanZeBut.selected=NO;
    if ([[_selectAry[_selectIndex] objectForKey:[NSString stringWithFormat:@"%d",(int)indexPath.row]] isEqualToString:@"YES"]) {
        [cell.xuanZeBut setImage:[UIImage imageNamed:@"已选1"] forState:UIControlStateNormal];
        
        
    }else{
        [cell.xuanZeBut setImage:[UIImage imageNamed:@"未选"] forState:UIControlStateNormal];
        
    }
    //[cell.xuanZeBut setBackgroundImage:[UIImage imageNamed:@"未选"] forState:UIControlStateNormal];
    // [cell.xuanZeBut setBackgroundImage:[UIImage imageNamed:@"已选1"] forState:UIControlStateSelected];
    cell.xuanZeBut.tag=indexPath.row;
    [cell.xuanZeBut addTarget:self action:@selector(xuanZeButClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    return cell;
    //  }
    
    
}
-(void)xuanZeButClick:(UIButton*)sender
{
    
    if ([[_selectAry[_selectIndex] objectForKey:[NSString stringWithFormat:@"%d",(int)sender.tag]] isEqualToString:@"YES"]) {
        
        
        sender.selected = YES;
        
    }
    else{
        sender.selected = NO;
    }
    
    sender.selected=!sender.selected;
    if (sender.selected) {
        
        
        [_selectAry[_selectIndex] removeObjectForKey:[NSString stringWithFormat:@"%d",(int)sender.tag]];
        [_selectAry[_selectIndex] setObject:@"YES" forKey:[NSString stringWithFormat:@"%d",(int)sender.tag ]];
        [sender setImage:[UIImage imageNamed:@"已选1"] forState:UIControlStateSelected];
        
        
        SecondShangPinGuanLi*sec=_dataAry[sender.tag];
        NSString*productS=[NSString stringWithFormat:@"%d",sec.productId];
        NSLog(@"productid  %d",sec.productId);
        [_xuanZhongAry addObject:productS];
        NSLog(@"---%lu",(unsigned long)_xuanZhongAry.count);
        
        
        
        
        
    }else {
        SecondShangPinGuanLi*sec=_dataAry[sender.tag];
        NSString*pr=[NSString stringWithFormat:@"%d",sec.productId];
        [_xuanZhongAry removeObject:pr];
        NSLog(@"111%@",_xuanZhongAry);
        [_selectAry[_selectIndex] removeObjectForKey:[NSString stringWithFormat:@"%d",(int)sender.tag ]];
        [_selectAry[_selectIndex] setObject:@"NO" forKey:[NSString stringWithFormat:@"%d",(int)sender.tag ]];
        [sender setImage:[UIImage imageNamed:@"未选"] forState:UIControlStateNormal];
        
    }
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return _dataAry.count;
    
    
}
-(void)textFieldBut
{
    SearchViewController*vc=[[SearchViewController alloc]init];
    [self presentViewController:vc animated:YES completion:^{
        nil;
    }];
}
-(void)backButClick
{
   [self dismissViewControllerAnimated:YES completion:^{
       nil;
   }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
